package com.ny.dao.impl;
import com.ny.until.DbUtil;
import com.ny.dao.ProductDao;
import com.ny.entity.Product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    private DbUtil dbUtil = new DbUtil();
    @Override
    public List<Product> selectAllProduct() throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "SELECT * FROM products";
        List<Product> productList = new ArrayList<>();

        try (PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            while (resultSet.next()) {
                int id = resultSet.getInt("product_id");
                String name = resultSet.getString("name");
                double price = resultSet.getDouble("price");
                int stock = resultSet.getInt("stock");
                String category = resultSet.getString("category");
                String description = resultSet.getString("description");

                Product product = new Product();
                product.setProductId(id);
                product.setProductName(name);
                product.setPrice(price);
                product.setStock(stock);
                product.setCategory(category);
                product.setDescription(description);
                productList.add(product);
            }
        }
        return productList;
    }
    @Override
    public Product getProductById(int productId) throws SQLException {
        String sql = "SELECT * FROM products WHERE product_id = ?";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setInt(1, productId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Product product = new Product();
                    product.setProductId(rs.getInt("product_id"));
                    product.setProductName(rs.getString("name"));
                    product.setPrice(rs.getDouble("price"));
                    product.setStock(rs.getInt("stock"));
                    product.setCategory(rs.getString("category"));
                    product.setDescription(rs.getString("description"));
                    return product;
                }
            }
        }
        return null;
    }

    @Override
    public int insertProduct(Product product) throws SQLException {
        String sql = "INSERT INTO products(name, price, stock, category, description) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, product.getProductName());
            ps.setDouble(2, product.getPrice());
            ps.setInt(3, product.getStock());
            ps.setString(4, product.getCategory());
            ps.setString(5, product.getDescription());

            return ps.executeUpdate();
        }
    }

    @Override
    public int updateProduct(Product product) throws SQLException {
        String sql = "UPDATE products SET name = ?, price = ?, stock = ?, category = ?, description = ? WHERE product_id = ?";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, product.getProductName());
            ps.setDouble(2, product.getPrice());
            ps.setInt(3, product.getStock());
            ps.setString(4, product.getCategory());
            ps.setString(5, product.getDescription());
            ps.setInt(6, product.getProductId());

            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteProduct(int productId) throws SQLException {
        String sql = "DELETE FROM products WHERE product_id = ?";
        try (Connection conn = dbUtil.getCon();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setInt(1, productId);
            return ps.executeUpdate();
        }
    }
}
